package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import util.DateJsonValueProcessor;
import bean.AppVo;
import bean.Comment;
import bean.CommentsVO;
import dao.AppDao;
import dao.TokenDao;

@SuppressWarnings("serial")
public class AppServlet extends HttpServlet
{
  private AppDao dao;
  private TokenDao tokenDao;

  @SuppressWarnings("unchecked")
protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    request.setCharacterEncoding("utf-8");
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    String tokenId = request.getParameter("tokenId");
    this.tokenDao = new TokenDao();
    String userId = tokenDao.findUserIdByToken(tokenId);
    StringBuffer stringJson = new StringBuffer();
    if(userId!=null){
    	this.dao = new AppDao();
    	List<AppVo> list = this.dao.findAppEntry(userId);
//    	List<AppVo> listApp = new ArrayList<AppVo>();
//    	for(int i=0;i<list.size();i++){
//    		JSONArray jsonArrays = null;
//    		AppVo app = (AppVo)list.get(i);
//    		List listScore = this.dao.feedCommentScoreList(app.getId());
//    		Comment comment = null;
//    		Integer count = 0;//评论星级次数
//			Integer counts = 0;//评论总分
//			String countScore = "0";//
//			String appId = null;
//    		for(int k=0;k<listScore.size();k++){
//    			comment = (Comment) listScore.get(k);
//    			count+=comment.getScoreCount();
//				counts+=comment.getCounts();
//				appId = comment.getAppId();
//    		}
//    		if(count!=0){
//				 String parten = "#.#";
//				 DecimalFormat decimal = new DecimalFormat(parten);
//				 countScore = decimal.format(counts/count);
//			}
//			if(app.getId().equals(appId)){
//    			app.setCount(count);
//    			app.setCountScore(Integer.valueOf(countScore).intValue());
//    		}
//    		
//    		jsonArrays = JSONArray.fromObject(listScore);
//    		app.setScore(jsonArrays.toString());
//    		listApp.add(app);
//    	}
    	JsonConfig config=new JsonConfig();
		config.registerJsonValueProcessor(Timestamp.class, new DateJsonValueProcessor(DateJsonValueProcessor.Default_DATE_PATTERN));
		JSONArray jsonArray = JSONArray.fromObject((List)list,config);
        stringJson.append("{ \"status\": { \"code\": \"0\",\"msg\": \"成功\"},");
        stringJson.append("\"data\": " + jsonArray + "}");
    }else{
    	stringJson.append("{ \"status\": { \"code\": \"1\",\"msg\": \"失败\"},");
        stringJson.append("\"data\": \"\"}");
    }
    
    try {
      PrintWriter writer = response.getWriter();
      writer.write(stringJson.toString());
      writer.flush();
      writer.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}